﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class WorkOrderIssueListControl : RSBaseUserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnAddIssues_Click(Object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int companyid = userIdentity.RSCompanyId;
                int WorkOrderid = hfWorkOrderId.Value == "" ? 0 : Convert.ToInt32(hfWorkOrderId.Value);
                if (companyid > 0 && WorkOrderid > 0)
                    ucModalWorkOrderIssueControl.ShowModal(companyid, WorkOrderid, 0);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);

        }
        public void LoadWorkOrderIssueListByAll(int companyid, string sql_and = "")
        {
            lblIssue.Text = "Status";
            ddlIssueStatus.Visible = true;
            btnAddIssues.Visible = false;
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            sql_and = sql_and.Trim();

            if (!Page.IsPostBack)
            {
                RSSBMDataLookupLoader.LoadStatusDDL(companyid, "ISSUE_CAT", ddlIssueStatus, "All", "0");
                ddlIssueStatus.SelectedValue = "0";
            }

            String sqlSELECT = "SELECT i.IssueId, i.IssueName, i.Description, i.Response, i.IssueDT, " +
                                "i.AssignTo, i.WorkOrderId, i.LocationId, i.StatusId, i.CompanyId,  " +
                                "(SELECT s.StatusName FROM rs_tblStatus s  " +
                                "WHERE s.StatusId=i.statusid AND s.category='ISSUE_CAT' AND s.CompanyId=@companyid) as Status " +
                                "FROM rs_tblIssue i WHERE i.CompanyId=@companyid " + sql_and + " ORDER BY i.IssueDT DESC";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvWorkOrderIssueList.DataSource = ds;
            gvWorkOrderIssueList.DataBind();
            myConnection.Close();
        }
        public void LoadWorkOrderIssueListByID(int companyid, int WorkOrderid)
        {
            btnAddIssues.Visible = true;
            lblIssue.Text = "WorkOrder Issues List";
            hfWorkOrderId.Value = WorkOrderid.ToString();

            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "SELECT i.IssueId,i.IssueName,i.Description,i.Response,i.IssueDT,i.AssignTo,i.StatusId,i.WorkOrderId,i.LocationId,i.CompanyId, (select s.statusname from rs_tblStatus s where s.statusid=i.statusid) as Status FROM rs_tblIssue i WHERE i.workorderid=@WorkOrderId AND i.CompanyId=@CompanyId ORDER BY i.IssueDT DESC";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@WorkOrderid";
            param.Value = WorkOrderid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvWorkOrderIssueList.DataSource = ds;
            gvWorkOrderIssueList.DataBind();
            myConnection.Close();
        }

        protected void gvWorkOrderIssue_Edit(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            int issueid = Convert.ToInt32(e.CommandArgument.ToString());
            int WorkOrderid = hfWorkOrderId.Value == "" ? 0 : Convert.ToInt32(hfWorkOrderId.Value);
            if (WorkOrderid == 0)
                WorkOrderid = RSSBMCommonFunctions.GetWorkOrderIDByIssueID(companyid, issueid);

            if (companyid > 0 && WorkOrderid > 0 && issueid > 0)
                ucModalWorkOrderIssueControl.ShowModal(companyid, WorkOrderid, issueid);
        }
        protected void gvWorkOrderIssue_Delete(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int issueid = Convert.ToInt32(e.CommandArgument.ToString());

                if (companyid > 0 && issueid > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@issueid", SqlDbType.Int, 0, ParameterDirection.Input, issueid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblIssue WHERE issueid=@issueid AND companyid=@companyid");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);
                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    ucModalActionMessageControl.ShowModal("Issue deleted successfully.", Request.RawUrl, true);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
        protected void ddlIssueStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            string sql_and = "";
            int isel = Convert.ToInt32(ddlIssueStatus.SelectedValue);
            if (isel > 0)
                sql_and = "AND StatusId=" + isel;
            LoadWorkOrderIssueListByAll(companyid, sql_and);
        }
    }

    
}